	package browser.service;

	import browser.dao.UserInfo;
	import java.io.*;
	import java.sql.*;
	import javax.servlet.*;
	import javax.servlet.http.*;

	/**
	 * Servlet that will show all the data in the specified view
	 *
	 */
	public class GetCurrentView extends HttpServlet {

		public static Connection conn;
		public static Statement stmt;

		public void doGet(HttpServletRequest request,
				HttpServletResponse response) throws ServletException,
				IOException {

			doPost(request, response);
		}

		public void doPost(HttpServletRequest request,
				HttpServletResponse response) throws ServletException,
				IOException {

			response.setContentType("text/html");
			PrintWriter out = response.getWriter();

			// Check if session is expired
			if (request.getSession(false) == null
					|| (Boolean) request.getSession(false).getAttribute(
							"expired") == true) {
				out.println("Session expired");
				return;
			}

			String[] uid = request.getParameterValues("uid");
			String[] uacc = request.getParameterValues("uAccess");
			String[] vname = request.getParameterValues("vName");

			try {
				Class.forName("oracle.jdbc.driver.OracleDriver");
			} catch (ClassNotFoundException e) {
				out.println("Error loading the Driver:" + e.getMessage());
				return;
			}

			Connection conn = null;
			UserInfo user = MyUtilities.getUserInfo(uid[0]);

			if ((user.getUaccess() == null)
					|| (!uacc[0].equals(user.getUaccess()))) {
				out.print("Please login first");
				return;
			}
			try {
				conn = DriverManager.getConnection("jdbc:oracle:thin:"
						+ user.getUid() + "/" + user.getPassword()
						+ "@oraserver.cs.luc.edu:1521:cs");
			} catch (SQLException e1) {
				out.println("Error connecting to Oracle:" + e1.getMessage());
				return;
			}
			if (conn == null) {
				out.println("Null Connection");
				return;
			}

			Statement stmt = null;
			try {
				stmt = conn.createStatement();
			} catch (SQLException e) {
				out.println("createStatement " + e.getMessage());
				try {
					conn.close();
				} catch (SQLException e2) {
				}
				;
				return;
			}

			out.println("<html>");
			out.println("  <head><title>" + vname[0]);
			out.println("  </title><link type=\"text/css\" rel=\"stylesheet\" href=\"main.css\"/></head>");
			out.println("  <body>");
			out.println("    <p>");
			out.println("    <center>");
			out.println("      <h2>" + vname[0] + "</h2>");

			ResultSet rset = null;
			/*
			 * String query =
			 * "select column_name from user_tab_columns where table_name='"
			 * +vname[0]+"'"; try { rset = stmt.executeQuery(query); }catch
			 * (SQLException e) { out.println("executeQuery " + e.getMessage());
			 * return; }
			 *
			 * try { out.println("<table border=2>"); out.println("<tr>");
			 * while(rset.next()){
			 * out.println("<th>"+rset.getString("column_name")+"</th>"); }
			 * out.println("</tr>");
			 *
			 * } catch (SQLException e) { e.printStackTrace(); }
			 */
			String query = "select * from " + vname[0];
			try {
				rset = stmt.executeQuery(query);
			} catch (SQLException e) {
				out.println("executeQuery " + e.getMessage());
				return;
			}

			try {
				out.println("<table border=2 id=\"table\">");
				out.println("<tr>");
				for (int i = 1; i <= rset.getMetaData().getColumnCount(); i++) {
					out.println("<th>" + rset.getMetaData().getColumnLabel(i)
							+ "</th>");
				}
				out.println("</tr>");
				while (rset.next()) {
					out.println("<tr>");
					for (int i = 1; i <= rset.getMetaData().getColumnCount(); i++) {
						out.println("<td>" + rset.getString(i) + "</td>");
					}
					out.println("</tr>");
				}
				out.println("</table>");

			} catch (SQLException e) {
				e.printStackTrace();
			}

			out.println("    </center>");
			out.println("    </p>");
			out.println("  </body>");
			out.println("</html>");

			try {
				stmt.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			out.close();
		}
	}
